﻿using StudyKitLib;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainModule
{
    public partial class ContainerBox : Form
    {
        public static long PortNO;
        public static RF1356.REPLY reply;
        ObservableCollection<string> RFIDUID = new ObservableCollection<string>();
        public ContainerBox()
        {
            InitializeComponent();
        }

        private void btnPortOpen_Click(object sender, EventArgs e)
        {
            try
            {
                int port = cmbPort.SelectedIndex + 1;

                if (!RF1356.RFReaderOpen(port))
                {
                    MessageBox.Show("COM Port Open Fail", "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation,
                                MessageBoxDefaultButton.Button1);
                }
                else
                {
                    cmbPort.Enabled = false;
                    //btnAutoOpen.Enabled = false;
                    btnPortOpen.Enabled = false;
                    btnPortClose.Enabled = true;
                    btnReadSerialNO.Enabled = true;
                    chkAuto.Enabled = true;
                    PortNO = port;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnReadSerialNO_Click(object sender, EventArgs e)
        {
            int tagCnt = 0;
            string SerialNumbers = String.Empty;

            grdView1.Rows.Clear();
            //lblCount.Text = "00";

            reply = RF1356.RFReadSerialNO(PortNO, ref SerialNumbers);

            switch (reply)
            {
                case RF1356.REPLY.RF_OK:
                    //lblCount.Text = SerialNumbers.Substring(0, 2);
                    tagCnt = int.Parse(SerialNumbers.Substring(0, 2));

                    for (int i = 0; i < tagCnt; i++)
                    {
                        string str = SerialNumbers.Substring(2 + (16 * i), 16);
                        string UID = String.Empty;
                        string UIDED = string.Empty;
                        string Maker = String.Empty;

                        for (int j = 0; j < 8; j++)
                        {
                            UID = str.Substring(2 * j, 2) + UID;
                        }

                        Maker = UID.Substring(2, 2);

                        switch (Maker)
                        {
                            case "04":
                                Maker = "PHILIPS";
                                break;

                            case "05":
                                Maker = "INFINEON";
                                break;

                            case "07":
                                Maker = "TI";
                                break;

                            default:
                                break;
                        }

                        grdView1.Rows.Add(new string[] { UID, Maker }); //lstUID1.Items.Add(UID); 
                        foreach (var kkk in RFIDUID)
                        {
                            if (kkk != UID)
                            {
                                RFIDUID.Add(UIDED);
                            }
                            else
                            {
                                MessageBox.Show("같네요");
                            }
                        }



                        if ((i % 2) == 0)
                        {
                            grdView1[0, i].Style.BackColor = Color.Aqua;
                            grdView1[1, i].Style.BackColor = Color.Aqua;
                        }
                        else
                        {
                            grdView1[0, i].Style.BackColor = Color.LightSteelBlue;
                            grdView1[1, i].Style.BackColor = Color.LightSteelBlue;
                        }
                    }

                    //if (chkSnd.Checked)
                    //    PlaySuccess();

                    //if (!chkAuto.Checked)
                    //{
                    //    btnReadData.Enabled = true;
                    //    btnWriteData.Enabled = true;
                    //}
                    break;

                case RF1356.REPLY.NO_TRANSPONDER:
                    //if (chkSnd.Checked)
                    //    PlayFail();
                    grdView1.Rows.Add(new string[] { "No Tag", "" });
                    break;

                case RF1356.REPLY.RF_ERR:
                    //if (chkSnd.Checked)
                    //    PlayFail();
                    grdView1.Rows.Add(new string[] { "Error", "" });
                    break;

                default:
                    break;
            }
        }

        private void ContainerBox_Load(object sender, EventArgs e)
        {
            int i;

            for (i = 1; i < 11; i++)    // COM1~10            
                cmbPort.Items.Add("COM" + i.ToString());

            for (i = 1; i < 11; i++)    // Timer Interval : 100~1000            
                cmbInterval.Items.Add((i * 100).ToString());

            cmbPort.SelectedIndex = 0;
            cmbInterval.SelectedIndex = 4;
        }

        private void btnPortClose_Click(object sender, EventArgs e)
        {
            try
            {
                if (RF1356.RFReaderClose(PortNO))
                {
                    //btnAutoOpen.Enabled = true;
                    btnPortOpen.Enabled = true;
                    btnPortClose.Enabled = false;
                    btnReadSerialNO.Enabled = false;
                   // btnReadData.Enabled = false;
                   // btnWriteData.Enabled = false;
                    cmbPort.Enabled = true;
                    chkAuto.Enabled = false;

                    try
                    {
                        if (RF1356.RFReaderIsOpen(PortNO))
                            RF1356.RFReaderClose(PortNO);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
